#ifndef __ONVIF_SERVER_H_
#define __ONVIF_SERVER_H_

#include "soapStub.h"

#define ONVIF_PTZ_TOKEN         "PtzToken"
#define ONVIF_PTZ_NAME          "PtzName"
#define ONVIF_PTZ_PRESET_NUM	32
#define ONVIF_TOKEN_LEN         128
#define ONVIF_ADDRESS_LEN		128
#define ONVIF_PROFILE_NUM			2
#define ONVIF_VIDEO_SOURCE_NUM		1
#define ONVIF_VIDEO_SOURCE_CFG_NUM	2
#define ONVIF_VIDEO_ENCODER_CFG_NUM	2

#define ONVIF_AUDIO_SOURCE_NUM		1
#define ONVIF_AUDIO_SOURCE_CFG_NUM	1
#define ONVIF_AUDIO_ENCODER_CFG_NUM	1

#define ONVIF_PTZ_CFG_NUM			1
#define ONVIF_DEVICE_SERVICE_ADDR_SIZE	80
#define ONVIF_SCOPES_STR_LEN 		128
#define ONVIF_SCOPES_NUM 			9

#define ONVIF_MEDIA_MAIN_PROFILE_NAME			"MainStreamProfile"
#define ONVIF_MEDIA_MAIN_PROFILE_TOKEN			"MainStreamProfileToken"
#define ONVIF_MEDIA_SUB_PROFILE_NAME			"SubStreamProfile"
#define ONVIF_MEDIA_SUB_PROFILE_TOKEN			"SubStreamProfileToken"

#define ONVIF_MEDIA_VIDEO_SOURCE_NAME			"VideoSourceName"
#define ONVIF_MEDIA_VIDEO_SOURCE_TOKEN			"VideoSourceToken"

#define ONVIF_MEDIA_AUDIO_SOURCE_NAME			"AudioSourceName"
#define ONVIF_MEDIA_AUDIO_SOURCE_TOKEN			"AudioSourceToken"

#define ONVIF_MEDIA_VIDEO_SOURCE_NAME			"VideoSourceName"
#define ONVIF_MEDIA_VIDEO_SOURCE_TOKEN			"VideoSourceToken"

#define ONVIF_MEDIA_VIDEO_MAIN_CFG_NAME			"VideoSourceConfig"
#define ONVIF_MEDIA_VIDEO_MAIN_CFG_TOKEN		"VideoSourceConfigToken"
#define ONVIF_MEDIA_VIDEO_SUB_CFG_NAME			"VideoSourceConfig"
#define ONVIF_MEDIA_VIDEO_SUB_CFG_TOKEN			"VideoSourceConfigToken"

#define ONVIF_MEDIA_VIDEO_MAIN_ENCODER_NAME		"VideoEncodeConfig_m"
#define ONVIF_MEDIA_VIDEO_MAIN_ENCODER_TOKEN	"VideoEncodeConfigToken_m"
#define ONVIF_MEDIA_VIDEO_SUB_ENCODER_NAME		"VideoEncodeConfig_s"
#define ONVIF_MEDIA_VIDEO_SUB_ENCODER_TOKEN		"VideoEncodeConfigToken_s"

#define ONVIF_MEDIA_AUDIO_CFG_NAME				"AudioSourceConfigureName"
#define ONVIF_MEDIA_AUDIO_CFG_TOKEN				"AudioSourceConfigureToken"

#define ONVIF_MEDIA_AUDIO_ENCODER_NAME			"AAC"
#define ONVIF_MEDIA_AUDIO_ENCODER_TOKEN			"AAC"

/* WS-Discovery UDP Multicast Address & Port */
#define ONVIF_WSD_MC_ADDR               "239.255.255.250"

#define ALLOC_STRUCT(val, type) {\
       val = (type *) soap_malloc(soap, sizeof(type)); \
	if(val == NULL) \
	{\
		printf("malloc err\r\n");\
		return SOAP_FAULT;\
	}\
	memset(val, 0, sizeof(type)); \
}

#define ALLOC_STR(val, size) {\
       val = (char *) soap_malloc(soap, size); \
	if(val == NULL) \
	{\
		printf("malloc err\r\n");\
		return SOAP_FAULT;\
	}\
	memset(val, 0, size); \
}

#define ALLOC_TOKEN(val, token) {\
	val = (char *) soap_malloc(soap, sizeof(char)*ONVIF_TOKEN_LEN); \
	if(val == NULL) \
	{\
		printf("malloc err\r\n");\
		return SOAP_FAULT;\
	}\
	memset(val, 0, sizeof(char)*ONVIF_TOKEN_LEN); \
	strcpy(val, token); \
	}

typedef struct _HI_ONVIF_PROFILE_S_
{
	char* Name;	/* required element of type tt:Name */
	char* token;	/* required attribute of type tt:ReferenceToken */
	int fixed;			/* optional attribute of type xsd:boolean */
	char streamUri[ONVIF_ADDRESS_LEN];

	int videoSourceCfgIdx;
	int videoEncoderCfgIdx;
	int audioSourceCfgIdx;
	int audioEncoderCfgIdx;
	int ptzCfgIdx;
} HI_ONVIF_PROFILE_S;

typedef struct _HI_ONVIF_VIDEO_SOURCE_S_
{
	char* token;	/* required attribute of type tt:ReferenceToken */
	float Framerate;	/* required element of type xsd:float */
	struct tt__VideoResolution
		Resolution;	/* required element of type tt:VideoResolution */
} HI_ONVIF_VIDEO_SOURCE_S;

typedef struct _HI_ONVIF_AUDIO_SOURCE_S_
{
	char* token;	/* required attribute of type tt:ReferenceToken */
	int Channels;	/* required element of type xsd:int */
} HI_ONVIF_AUDIO_SOURCE_S;

typedef struct _HI_ONVIF_VIDEO_SOURCE_CFG_S_
{
	char* Name;	/* required element of type tt:Name */
	int UseCount;	/* required element of type xsd:int */
	char* token;	/* required attribute of type tt:ReferenceToken */
	char* SourceToken;	/* required element of type tt:ReferenceToken */
	struct tt__IntRectangle Bounds;	/* required element of type tt:IntRectangle */
} HI_ONVIF_VIDEO_SOURCE_CFG_S;

typedef struct _HI_ONVIF_AUDIO_SOURCE_CFG_S_
{
	char* Name;	/* required element of type tt:Name */
	int UseCount;	/* required element of type xsd:int */
	char* token;	/* required attribute of type tt:ReferenceToken */
	char* SourceToken;	/* required element of type tt:ReferenceToken */
} HI_ONVIF_AUDIO_SOURCE_CFG_S;

typedef struct _HI_ONVIF_VIDEO_ENCODER_CFG_S_
{
	char* Name;	/* required element of type tt:Name */
	int UseCount;	/* required element of type xsd:int */
	char* token;	/* required attribute of type tt:ReferenceToken */
	enum tt__VideoEncoding Encoding;	/* required element of type tt:VideoEncoding */
	struct tt__VideoResolution
		Resolution;	/* required element of type tt:VideoResolution */
	float Quality;	/* required element of type xsd:float */
	char* SessionTimeout;	/* external */
	int Gop;
	int FrameRate;
	int Bitrate;
	int AdvanceEncType;  //视频编码格式0--h264 1--MJPEG 2--JPEG	3--MPEG4 4--h265
} HI_ONVIF_VIDEO_ENCODER_CFG_S;

typedef struct _HI_ONVIF_AUDIO_ENCODER_CFG_S_
{
	char* Name;	/* required element of type tt:Name */
	int UseCount;	/* required element of type xsd:int */
	char* token;	/* required attribute of type tt:ReferenceToken */
	enum tt__AudioEncoding Encoding;	/* required element of type tt:AudioEncoding */
	int Bitrate;	/* required element of type xsd:int */
	int SampleRate;	/* required element of type xsd:int */
	char* SessionTimeout;	/* external */
} HI_ONVIF_AUDIO_ENCODER_CFG_S;

typedef struct _HI_ONVIF_PTZ_PRESET_CFG_S_
{
	char Name[ONVIF_TOKEN_LEN];	/* required element of type tt:Name */
	char token[ONVIF_TOKEN_LEN];	/* required attribute of type tt:ReferenceToken */
} HI_ONVIF_PTZ_PRESET_CFG_S;

typedef struct _HI_ONVIF_PTZ_CFG_S_
{
	HI_ONVIF_PTZ_PRESET_CFG_S presetCfg[ONVIF_PTZ_PRESET_NUM];
} HI_ONVIF_PTZ_CFG_S;

typedef struct _HI_ONVIF_SCOPE_S_
{
	int isFixed;
	char scope[ONVIF_SCOPES_STR_LEN];
} HI_ONVIF_SCOPE_S;

typedef struct _HI_ONVIF_MEDIA_CFG_S_
{
	struct tt__VideoResolution h264Options[2];
	struct tt__VideoResolution VideoResolutionconfig;
	struct tt__VideoRateControl VideoEncoderRateControl;

} HI_ONVIF_MEDIA_CFG_S;

typedef struct _HI_ONVIF_INFO_S_
{
	int HttpPort;

	HI_ONVIF_PROFILE_S OnvifProfile[ONVIF_PROFILE_NUM];

	HI_ONVIF_VIDEO_SOURCE_S OnvifVideoSource[ONVIF_VIDEO_SOURCE_NUM];
	HI_ONVIF_AUDIO_SOURCE_S OnvifAudioSource[ONVIF_AUDIO_SOURCE_NUM];

	HI_ONVIF_VIDEO_SOURCE_CFG_S
	OnvifVideoSourceCfg[ONVIF_VIDEO_SOURCE_CFG_NUM];	/* optional element of type tt:VideoSourceConfiguration */
	HI_ONVIF_AUDIO_SOURCE_CFG_S
	OnvifAudioSourceCfg[ONVIF_AUDIO_SOURCE_CFG_NUM];	/* optional element of type tt:AudioSourceConfiguration */
	HI_ONVIF_VIDEO_ENCODER_CFG_S
	OnvifVideoEncoderCfg[ONVIF_VIDEO_ENCODER_CFG_NUM];	/* optional element of type tt:VideoEncoderConfiguration */
	HI_ONVIF_AUDIO_ENCODER_CFG_S
	OnvifAudioEncoderCfg[ONVIF_AUDIO_ENCODER_CFG_NUM];	/* optional element of type tt:AudioEncoderConfiguration */
	HI_ONVIF_PTZ_CFG_S OnvifPTZCfg[ONVIF_PTZ_CFG_NUM];

	//struct tt__Profile Profile[ONVIF_PROFILE_NUM];
	//struct tt__VideoSource VideoSource[ONVIF_VIDEO_SOURCE_NUM];
	//struct tt__AudioSource AudioSource[ONVIF_AUDIO_SOURCE_NUM];
	//struct tt__VideoSourceConfiguration VideoSourceConfiguration[ONVIF_VIDEO_SOURCE_CFG_NUM];	/* optional element of type tt:VideoSourceConfiguration */
	//struct tt__AudioSourceConfiguration AudioSourceConfiguration[ONVIF_AUDIO_SOURCE_CFG_NUM];	/* optional element of type tt:AudioSourceConfiguration */
	//struct tt__VideoEncoderConfiguration VideoEncoderConfiguration[ONVIF_VIDEO_ENCODER_CFG_NUM];	/* optional element of type tt:VideoEncoderConfiguration */
	//struct tt__AudioEncoderConfiguration AudioEncoderConfiguration[ONVIF_AUDIO_ENCODER_CFG_NUM];	/* optional element of type tt:AudioEncoderConfiguration */

	/* Service Addresses */
	char device_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char deviceio_xaddr[ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char events_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char media_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char imaging_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char ptz_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char analytics_xaddr [ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char recordingcontrol_xaddr[ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char recordingsearch_xaddr[ONVIF_DEVICE_SERVICE_ADDR_SIZE];
	char replay_xaddr[ONVIF_DEVICE_SERVICE_ADDR_SIZE];

	HI_ONVIF_SCOPE_S type_scope[ONVIF_SCOPES_NUM];
	HI_ONVIF_SCOPE_S name_scope[ONVIF_SCOPES_NUM];
	HI_ONVIF_SCOPE_S location_scope[ONVIF_SCOPES_NUM];
	HI_ONVIF_SCOPE_S hardware_scope[ONVIF_SCOPES_NUM];

	HI_ONVIF_MEDIA_CFG_S MediaConfig;

} HI_ONVIF_INFO_S;

extern HI_ONVIF_INFO_S gOnvifInfo;

void *OnvifBeDiscovered(void *arg) ;
void *OnvifWebServices(void *arg) ;
int onvif_server_init(void);
int onvif_init(void);
#endif